from bs4 import BeautifulSoup
import requests

for start_num in range(1, 51):

    # 获取html 源码
    content = requests.get(f"https://books.toscrape.com/catalogue/page-{start_num}.html").text
    soup = BeautifulSoup(content, "html.parser")

    # 存储价格和书名
    book_price = []
    book_name = []
    count = 0
    # 在源码中获取价格
    all_price = soup.find_all('p', attrs= {"class" : "price_color"})
    for price in all_price:
        book_price.append(price.string[2:])


    # 在源码中获取书名
    all_links = soup.find_all("h3")
    for link in all_links:
        name = link.find("a")
        book_name.append(name.string)

    # 计算所有的书 并且输出
    lens = len(book_price)
    for count in range(lens):
        print("书名: ", book_name[count], " 价格: ", book_price[count])
        count += 1

    print(lens)